﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmPickingDetailSet : Form
    {
        public Action RefreshUiEvent;
        RackSetupBLL rackSetupBLL = new RackSetupBLL();
        PickingSetupBLL pickingSetupBLL = new PickingSetupBLL();
        PickingDetailBLL pickingDetailBLL = new PickingDetailBLL();
        PickingDetailModel _pickingDetailModel;

        public FrmPickingDetailSet()
        {
            InitializeComponent();

            var list = pickingSetupBLL.GetModelAll();
            cbPicking.DataSource = list;
            cbPicking.DisplayMember = "Name";
            cbPicking.ValueMember = "ID";
            cbPicking.SelectedIndex = 0;

            var list2 = rackSetupBLL.GetModelAll();
            cbRack.DataSource = list2;
            cbRack.DisplayMember = "Name";
            cbRack.ValueMember = "ID";
            cbRack.SelectedIndex = 0;
        }

        public FrmPickingDetailSet(PickingDetailModel model)
        {
            InitializeComponent();
            _pickingDetailModel = model;

            var list = pickingSetupBLL.GetModelAll();
            var pickingSetup = pickingSetupBLL.GetModel(_pickingDetailModel.PickingID);
            cbPicking.DataSource = list;
            cbPicking.DisplayMember = "Name";
            cbPicking.ValueMember = "ID";
            cbPicking.SelectedItem = pickingSetup;

            var list2 = rackSetupBLL.GetModelAll();
            var rackSetup = rackSetupBLL.GetModel(_pickingDetailModel.RackID);
            cbRack.DataSource = list2;
            cbRack.DisplayMember = "Name";
            cbRack.ValueMember = "ID";
            cbRack.SelectedItem = rackSetup;

            btnSave.Text = "修改";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_pickingDetailModel == null)
            {
                //var model = rackSetupBLL.GetModel(txtRackName.Text);
                //if (model != null)
                //{
                //    MessageBox.Show("已存在相同的货架配置，不能重复！");
                //    return;
                //}

                var picking = cbPicking.SelectedItem as PickingSetupModel;
                var rack = cbRack.SelectedItem as RackSetupModel;

                _pickingDetailModel = new PickingDetailModel();
                _pickingDetailModel.PickingID = picking.ID;
                _pickingDetailModel.RackID = rack.ID;
                var result = pickingDetailBLL.Add(_pickingDetailModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加拣货明细成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加拣货明细失败：" + result.Dsca);
            }
            else
            {
                var picking = cbPicking.SelectedItem as PickingSetupModel;
                var rack = cbRack.SelectedItem as RackSetupModel;

                _pickingDetailModel.PickingID = picking.ID;
                _pickingDetailModel.RackID = rack.ID;

                var result = pickingDetailBLL.Update(_pickingDetailModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改拣货明细成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改拣货明细失败：" + result.Dsca);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();   
        }
    }
}
